package _作业.Synchronized;

public class Tset02 {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(ticket,"张三").start();
        new Thread(ticket,"李四").start();
        new Thread(ticket,"王五").start();
    }
}

class Ticket02 implements Runnable{
    private int total= 100;
    static Object obj =new Object();
    @Override
    public void run() {

        while (true){
            synchronized (obj){
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (total>0){
                    total--;
                    System.out.println(Thread.currentThread().getName()+"购买了一张票，余票"+total);
                }else {
                    System.out.println("票卖完了，下次再来");
                    break;
                }


            }

        }

    }
}
